package com.wqr.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqr.pojo.Delicious;
import com.wqr.pojo.Discuss;
import com.wqr.pojo.Login;
import com.wqr.service.DiscussService;

@Controller
public class DiscussController {
	@Autowired
    public DiscussService discussService;
	
	@RequestMapping("/manageDiscuss")
	public String discuss(Model model,Discuss discuss) throws Exception {
		List<Discuss> list = discussService.selectDiscuss(discuss);
		model.addAttribute("itemList", list);
	    return "manageDiscuss"; 
	}
	@RequestMapping("/addDiscuss")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int addDiscuss(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			Discuss discuss,HttpSession session,Login login) throws Exception {
		//IsLogin(session);
		discuss.setDiscussTime(new Date());
		login = (Login)session.getAttribute("login");
		discuss.setLoginId(login.getLoginId());
		discuss.setLoginName(login.getLoginName());
		int a = discussService.insertDiscuss(discuss);
		return a;
	}
	@RequestMapping("/userAddDiscuss")
    public String userAddDiscuss(@RequestParam("deliciousId") Integer deliciousId,Model model) {
		model.addAttribute("deliciousId", deliciousId);
		return "userAddDiscuss";
    }
	//@RequestMapping(value="/deleteDiscuss/{userStateId}",method=RequestMethod.GET)
	@RequestMapping("/deleteDiscuss")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int deleteDiscuss(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			Discuss discuss) throws Exception {
		//IsLogin(session);
		int a = discussService.delectDiscuss(discuss.getDiscussId());
		return a;
	}
	@RequestMapping("/updateDiscuss")
	@ResponseBody
	public Discuss updateDiscuss(@RequestBody Discuss discuss) throws Exception {
		//IsLogin(session);
		Discuss itemList = discussService.selectByPrimaryKey(discuss.getDiscussId());
		return itemList;
	}
	@RequestMapping("/saveDiscuss")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int saveDiscuss(@RequestBody Discuss discuss) throws Exception {
		//IsLogin(session);
		int a = discussService.updateDiscuss(discuss);
		return a;
	}
	@RequestMapping("/searchDiscuss")
	@ResponseBody
	public List<Discuss> searchDiscuss(@RequestBody Discuss discuss) throws Exception {
		if(discuss.getDiscussContent() == ""){
			discuss.setDiscussContent(null);
		}
		
		List<Discuss> itemList = discussService.selectDiscuss(discuss);
	    return itemList;
	}
	@RequestMapping("/searchAllDiscuss")
	@ResponseBody
	public List<Discuss> searchAllDiscuss() throws Exception {
		List<Discuss> itemList = discussService.list();
	    return itemList;
	}
}
